<template>
  <div class='icons-box'>
    <swiper :options="swiperOption">
      <swiper-slide class="icons"
                    v-for="(page, index) of pages"
                    :key='index'>
        <div v-for="icon of page"
             :key='icon.id'
             class='icon'>
          <div class="icon-img">
            <img :src='icon.imgUrl'
                 :alt='icon.desc'>
          </div>
          <p class="icon-desc">{{icon.desc}}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
export default {
  name: 'HomeIcons',
  props: {
    list: Array
  },
  data () {
    return {
      swiperOption: {
        loop: false,
        autoplay: false
      }
    }
  },
  computed: {
    pages () {
      const pages = []
      this.list.forEach((item, index) => {
        const page = Math.floor(index / 8) // 向下取整
        if (!pages[page]) { // 如果pages[page]为空，就添加一个空数组，然后往空数组里面加数据，加满8条后，再次循环添加一个新的空数组
          pages[page] = []
        }
        pages[page].push(item)
      })
      return pages
    }
  }
}
</script>

<style lang='scss' scoped>
@import '~@sass/varibles/_color';
@import '~@sass/mixins/_global';
.icons-box >>> .swiper-container {
  height: 0;
  padding-bottom: 50%;
  overflow: hidden;
}
.icons {
  display: flex;
  flex-wrap: wrap;
}
.icon {
  width: 25%;
  padding-bottom: 25%;
  height: 0;
  position: relative;
}
.icon-img {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0.44rem;
  text-align: center;
  box-sizing: border-box;
  padding: 0.15rem 0.1rem 0.05rem;
  img {
    height: 100%;
    display: inline-block;
  }
}
.icon-desc {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 0.44rem;
  line-height: 0.44rem;
  box-sizing: border-box;
  text-align: center;
  color: $darkTextColor;
  @extend %text-overflow--e;
}
</style>
